import 'package:flutter/material.dart';
import 'dart:math' as Math;
import '../static_var.dart';

class LoadingWidget extends StatefulWidget {
  final bool hasBk;
  final double iconSize;
  LoadingWidget({this.hasBk = true, this.iconSize=24});

  @override
  _LoadingAnimState createState() {
    return _LoadingAnimState();
  }
}
class _LoadingAnimState extends State<LoadingWidget> with SingleTickerProviderStateMixin {
  AnimationController animationController;
  Animation<double> rotateAnimation;

  @override
  void initState() {
    super.initState();
    animationController = new AnimationController(duration: const Duration(seconds: 1), vsync: this);
    rotateAnimation = new Tween(begin: 0.0, end: Math.pi * 2).animate(animationController);
    rotateAnimation.addStatusListener((status){
      if (status == AnimationStatus.completed) {
        //当动画结束时重复动画
        animationController.repeat();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    animationController.forward();
    return Container(
      color: widget.hasBk ? Color.fromARGB(60, 0, 0, 0) : Colors.transparent,
      child: Center(
        child: AnimatedBuilder(
          //监听动画变化
          animation: rotateAnimation,
          child: Icon(Icons.cached, color: StaticVar.blueColor, size: widget.iconSize,),
          builder: (BuildContext context, Widget child) {
            return Transform.rotate(
              child: child,
              angle: rotateAnimation.value,
            );
          }
        )
      )
    );
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }
}